package com.gome.security.util.DSA;

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.Key;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.interfaces.DSAPublicKey;
import java.security.interfaces.DSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;

/**
 * DSA 安全编码组件
 * 
 * @author tx_hemaojie
 * 1、DSA的公钥长度略长于私钥，这个和RSA有较大差别。可以通过两个算法的控制台输出公钥私钥长度比对
2、DSA算法的签名长度与密钥长度无关。可能和待签名数据有联系...
 */
public abstract class DSACoder {
	// 数字签名秘钥算法
	public static final String ALGORITHM = "DSA";
	// 数字签名 算法
	public static final String SINGATURE_ALGORITHM = "SHA1withDSA";
	// 公钥
	public static final String PUBLIC_KEY = "DSAPublicKey";
	// 私钥
	public static final String PRIVATE_KEY = "DSAPrivateKey";
	/*
	 * DSA秘钥长度 默认1024 长度必须是64整数倍 范围在512-1024之间
	 */

	private static final int KEY_SIZE = 1024;

	/**
	 * 签名
	 * 
	 * @param data
	 *            待签名数据
	 * @param privateKey
	 *            私钥
	 * @return byte[] 数字签名
	 * @throws Exception
	 */
	public static byte[] sign(byte[] data, byte[] privateKey) throws Exception {
		// 还原私钥
		// 转换私钥材料
		PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);
		// 实例化密钥工厂
		KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
		// 生成私钥对象
		PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
		// 实例化Signature
		Signature signature = Signature.getInstance(SINGATURE_ALGORITHM);
		// 初始化Signature
		signature.initSign(priKey);
		// 更新
		signature.update(data);
		// 签名
		return signature.sign();
	}

	/**
	 * 校验
	 * 
	 * @param data
	 *            带校验数据
	 * @param publicKey
	 *            公钥
	 * @param sign
	 *            数字签名
	 * @return boolean 成功 ture 失败 false
	 * @throws Exception
	 */
	public static boolean verify(byte[] data, byte[] publicKey, byte[] sign) throws Exception {

		// 还原公钥
		// 转换公钥材料
		X509EncodedKeySpec KeySpec = new X509EncodedKeySpec(publicKey);
		// 实例化密钥工厂
		KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
		// 生成公钥对象
		PublicKey pubKey = keyFactory.generatePublic(KeySpec);
		// 实例化Signature
		Signature signature = Signature.getInstance(SINGATURE_ALGORITHM);
		// 初始化Signature
		signature.initVerify(pubKey);
		// 更新
		signature.update(data);
		// 验证
		return signature.verify(sign);

	}

	/**
	 * 生成密钥
	 * 
	 * @return 密钥对象
	 * @throws Exception
	 */
	public static Map<String, Object> initKey() throws Exception {
		// 初始化密钥生成器
		KeyPairGenerator keygen = KeyPairGenerator.getInstance(ALGORITHM);
		// 实例化密钥对生成器
		keygen.initialize(KEY_SIZE, new SecureRandom());
		// 实例化密钥对
		KeyPair keys = keygen.genKeyPair();
		DSAPublicKey publicKey = (DSAPublicKey) keys.getPublic();
		DSAPrivateKey privateKey = (DSAPrivateKey) keys.getPrivate();
		// 封装密钥
		Map<String, Object> map = new HashMap<String, Object>(2);
		map.put(PUBLIC_KEY, publicKey);
		map.put(PRIVATE_KEY, privateKey);
		
		
		return map;

	}

	/**
	 * 取得私钥
	 * 
	 * @param keyMap
	 *            密钥Map
	 * @return byte[] 私钥
	 * @throws Exception
	 */
	public static byte[] getPrivateKey(Map<String, Object> keyMap) throws Exception {
		Key key = (Key) keyMap.get(PRIVATE_KEY);
		return key.getEncoded();
	}

	/**
	 * 取得公钥
	 * 
	 * @param keyMap
	 *            密钥Map
	 * @return byte[] 公钥
	 * @throws Exception
	 */
	public static byte[] getPublicKey(Map<String, Object> keyMap) throws Exception {
		Key key = (Key) keyMap.get(PUBLIC_KEY);
		return key.getEncoded();
	}

}
